﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class SubjectDAO : IDAO<Subject>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(Subject entity)
        {
            agrDb.Subjects.AddObject(entity);
        }

        public void remove(Subject entity)
        {
            agrDb.Subjects.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (Subject subject in agrDb.Subjects.Where(s => s.SubjectCode == code))
            {
                agrDb.Subjects.DeleteObject(subject);
            }
        }

        public void merge(Subject entity)
        {
            foreach (Subject subject in agrDb.Subjects.Where(s => s.SubjectCode == entity.SubjectCode))
            {
                subject.SubjectCredits = entity.SubjectCredits;
                subject.SubjectLevel = entity.SubjectLevel;
                subject.SubjectTitle = entity.SubjectTitle;
                subject.SubjectType = entity.SubjectType;

            }

            //Select ALL
            //var subjects = (from s in agrDb.Subjects
            //                where s.SubjectCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var subjects2 = (from s in agrDb.Subjects
            //                select new
            //                {
            //                    s.SubjectCourses,
            //                    s.SubjectDisability
            //                });

            //Order rows
            //var orderedListByCourses = subjects.OrderBy(s => s.SubjectCourses);

        }

        public Subject findById(string code)
        {
            Subject subject = new Subject();

            foreach (Subject stud in agrDb.Subjects.Where(s => s.SubjectCode == code))
            {
                subject = stud;
            }

            return subject;
        }

        public IEnumerable<Subject> findAll()
        {
            var subjects = (from stud in agrDb.Subjects
                            select stud);

            return subjects;
        }

        public IEnumerable<Subject> findInRange(int firstResult, int maxResuslts)
        {
            var subjects = (from stud in agrDb.Subjects
                            select stud);

            return subjects.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var subjects = (from stud in agrDb.Subjects
                            select stud).ToList();

            return subjects.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}
